namespace Library
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Windows.Forms;

    [ToolboxBitmap(typeof(RotaryImage))]
    public class RotaryImage : Control
    {
        private float _angle = 0f;
        private bool _bHaveAction = false;
        private string _ClickEvent = null;
        private string _DoubleEvent = null;
        private Bitmap _image = null;
        private Bitmap _originalImage = null;
        private string _path = "";
        private string _rotateName = null;
        public bool bHavePic = false;
        private IContainer components = null;
        private Rectangle rectangele;

        public RotaryImage()
        {
            this.InitializeComponent();
            base.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        public static string ImageToString(Image img)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                img.Save(stream, ImageFormat.Bmp);
                return Convert.ToBase64String(stream.GetBuffer());
            }
        }

        private void InitializeComponent()
        {
            this.components = new Container();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            Pen pen;
            Rectangle rectangle3;
            Rectangle rectangle4;
            Graphics graphics = pe.Graphics;
            GraphicsPath path = new GraphicsPath();
            Rectangle rectangle = new Rectangle(0, 0, base.ClientSize.Width, base.ClientSize.Height);
            Rectangle rectangle2 = new Rectangle(new Point(0, 0), new Size(base.ClientSize.Width / 2, base.ClientSize.Height / 2));
            if (this._angle != 0f)
            {
                PointF point = new PointF((float) ((rectangle.Left + rectangle.Right) / 2), (float) ((rectangle.Top + rectangle.Bottom) / 2));
                if (this._path == null)
                {
                    pen = new Pen(Color.Black, -1f);
                    rectangle3 = new Rectangle(0, 0, base.ClientSize.Width - 1, base.ClientSize.Height - 1);
                    graphics.DrawRectangle(pen, rectangle3);
                }
                else if (this._path.Length == 0)
                {
                    pen = new Pen(Color.Black, -1f);
                    rectangle3 = new Rectangle(0, 0, base.ClientSize.Width - 1, base.ClientSize.Height - 1);
                    graphics.DrawRectangle(pen, rectangle3);
                }
                else
                {
                    if (!this.bHavePic)
                    {
                        this.bHavePic = true;
                        this.TheImage = (Bitmap) StringToImage(this._path);
                    }
                    using (Matrix matrix = new Matrix())
                    {
                        matrix.RotateAt(this._angle, point);
                        graphics.MultiplyTransform(matrix);
                        graphics.DrawImage(this._image, new Point(rectangle.X + (base.ClientSize.Width / 4), rectangle.Y + (base.ClientSize.Height / 4)));
                        if ((this.Rect.Height != (rectangle.Height / 2)) || (this.Rect.Width != (rectangle.Width / 2)))
                        {
                            this.ResizeImage(this.Rect.Width, this.Rect.Height);
                            rectangle4 = new Rectangle(rectangle.Location.X, rectangle.Location.Y, rectangle.Width / 2, rectangle.Height / 2);
                            this.Rect = rectangle4;
                        }
                    }
                }
            }
            else if (this._path == null)
            {
                pen = new Pen(Color.Black, -1f);
                rectangle3 = new Rectangle(0, 0, base.ClientSize.Width - 1, base.ClientSize.Height - 1);
                graphics.DrawRectangle(pen, rectangle3);
            }
            else if (this._path.Length == 0)
            {
                pen = new Pen(Color.Black, -1f);
                rectangle3 = new Rectangle(0, 0, base.ClientSize.Width - 1, base.ClientSize.Height - 1);
                graphics.DrawRectangle(pen, rectangle3);
            }
            else
            {
                if (!this.bHavePic)
                {
                    this.bHavePic = true;
                    this.TheImage = (Bitmap) StringToImage(this._path);
                }
                graphics.DrawImage(this._image, new Point(rectangle.X + (base.ClientSize.Width / 4), rectangle.Y + (base.ClientSize.Height / 4)));
                if ((this.Rect.Height != (rectangle.Height / 2)) || (this.Rect.Width != (rectangle.Width / 2)))
                {
                    this.ResizeImage(this.Rect.Width, this.Rect.Height);
                    rectangle4 = new Rectangle(rectangle.Location.X, rectangle.Location.Y, rectangle.Width / 2, rectangle.Height / 2);
                    this.Rect = rectangle4;
                }
            }
            base.OnPaint(pe);
        }

        protected void ResizeImage(int width, int height)
        {
            if (this._originalImage != null)
            {
                if (width < 1)
                {
                    width = 2;
                }
                if (height < 1)
                {
                    height = 2;
                }
                Bitmap bitmap = new Bitmap(this._originalImage, new Size(width, height));
                this._image = (Bitmap) bitmap.Clone();
                bitmap.Dispose();
                this.Path = ImageToString(this._image);
            }
        }

        public static Image StringToImage(string sz)
        {
            byte[] buffer = Convert.FromBase64String(sz);
            MemoryStream stream = new MemoryStream();
            stream.Write(buffer, 0, buffer.Length);
            Bitmap bitmap = new Bitmap(stream);
            bitmap.MakeTransparent();
            return Image.FromHbitmap(bitmap.GetHbitmap());
        }

        [Description("旋转角度"), Category("图片")]
        public float angle
        {
            get
            {
                return this._angle;
            }
            set
            {
                this._angle = value;
                base.Invalidate();
            }
        }

        public bool bHaveAction
        {
            get
            {
                return this._bHaveAction;
            }
            set
            {
                this._bHaveAction = value;
            }
        }

        public string ClickEvent
        {
            get
            {
                return this._ClickEvent;
            }
            set
            {
                this._ClickEvent = value;
            }
        }

        public string DoubleEvent
        {
            get
            {
                return this._DoubleEvent;
            }
            set
            {
                this._DoubleEvent = value;
            }
        }

        public string Path
        {
            get
            {
                return this._path;
            }
            set
            {
                this._path = value;
            }
        }

        public Rectangle Rect
        {
            get
            {
                return this.rectangele;
            }
            set
            {
                this.rectangele = value;
            }
        }

        public string rotateName
        {
            get
            {
                return this._rotateName;
            }
            set
            {
                this._rotateName = value;
            }
        }

        [Category("图片"), Description("选择一个图片")]
        public Bitmap TheImage
        {
            get
            {
                return this._image;
            }
            set
            {
                this.Rect = new Rectangle(0, 0, base.ClientSize.Width / 2, base.ClientSize.Height / 2);
                this._originalImage = value;
                this.ResizeImage(this.rectangele.Width, this.rectangele.Height);
                base.Invalidate();
            }
        }
    }
}

